package com.rambler.config.controller;

import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.Properties;

@RestController
@RequestMapping("/nacos")
public class NacosUploadController {

    @Value("${nacos.server-addr}")
    private String serverAddr;

    @Value("${nacos.namespace:public}")
    private String namespace;

    @PostMapping("/upload")
    public String uploadConfig(@RequestParam String dataId,
            @RequestParam(defaultValue = "DEFAULT_GROUP") String group,
            @RequestParam String content,
            @RequestParam(defaultValue = "yaml") String type) {
        try {
            Properties properties = new Properties();
            properties.setProperty("serverAddr", serverAddr);
            properties.setProperty("namespace", namespace);

            ConfigService configService = NacosFactory.createConfigService(properties);
            boolean success = configService.publishConfig(dataId, group, content, type);
            return success ? "✅ 上传成功" : "❌ 上传失败";

        } catch (NacosException e) {
            e.printStackTrace();
            return "❌ 出错: " + e.getErrMsg();
        }
    }
}
